<?php
namespace Home\Controller;
use Think\Controller;
class CommonController extends Controller{
    /**
     * 栏目ID
     * @var type 
     */
    public $TypeId;
    /**
     * 栏目信息
     * @var type 
     */
    public $TypeInfos;
    /**
     * 主页链接名
     * @var type 
     */
    public $indexName;
    /**
     * 网页主页链接
     * @var type 
     */
    public $indexUrl;
    /**
     * 栏目位置的间隔符号
     * @var type 
     */
    public $SplitSymbol;
    public $valuePosition;
    public $valuePositionName;
    /**
     * 全局初始化
     */
    public function _initialize(){
        // utf-8编码
        header('Content-Type: text/html; charset=utf-8');
        $this->indexUrl  = C("CFG_BASEHOST") . C('INDEX_URL');//$GLOBALS['cfg_basehost'].$GLOBALS['cfg_indexurl'];
        $this->indexName = C("INDEX_NAME");
        $this->SplitSymbol = C("LIST_SYMBOL");
        $this->valuePosition = '';
        $this->valuePositionName = '';
        //var_dump($this->TypeID);die;
        $df_lang = C("DEFAULT_LANG");
        $get_lang = I("get.l", NULL);
        if(empty($get_lang)){
            $get_lang = cookie('think_language') ? cookie('think_language') : $df_lang;
        }
        if(C("HOME_LANG_SWITCH_ON")){
            $langModel = D("Language");
            $default_theme = $langModel->field("theme")->where(array('status' => 1))->getByNid($get_lang);
            if(isArrayAndNotEmpty($default_theme)){
                $theme = $default_theme['theme'];
                if($theme != C("DEFAULT_THEME")){
                    C("DEFAULT_THEME", $theme, "default");
                }
            }
            //DEFAULT_LANG
            $this->indexName = L('index_name');
        }
        $data = array();
        //$data['position'] = $this->GetPositionLink(true);
        $data['title'] = C("SITE_TITLE");
        $data['keywords'] = C("SITE_KEYWORD");
        $data['description'] = C("SITE_DESCRIPTION");
        $this->assign($data);
        //var_dump($data);die;
    }
    
    /**
     * 获得某类目的链接列表 如：类目一>>类目二>> 这样的形式
     * @param type $islink  表示返回的列表是否带连接
     * @return string
     */
    protected function GetPositionLink($islink=true){
        $indexpage = "<a href='".$this->indexUrl."'>".$this->indexName."</a>";
        if($this->valuePosition!="" && $islink)
        {
            return $this->valuePosition;
        }
        else if($this->valuePositionName!="" && !$islink)
        {
            return $this->valuePositionName;
        }
        else if($this->TypeId==0)
        {
            if($islink)
            {
                return $indexpage;
            }
            else
            {
                return "没指定分类！";
            }
        }
        else
        {
            if($islink)
            {
                $this->valuePosition = $this->GetOneTypeLink($this->TypeInfos);
                if($this->TypeInfos['reid']!=0)
                {
                    //调用递归逻辑
                    $this->LogicGetPosition($this->TypeInfos['reid'],true);
                }
                $this->valuePosition = $indexpage.$this->SplitSymbol.$this->valuePosition;
                return $this->valuePosition.$this->SplitSymbol;
            }
            else
            {
                $this->valuePositionName = $this->TypeInfos['typename'];
                if($this->TypeInfos['reid']!=0)
                {
                    //调用递归逻辑
                    $this->LogicGetPosition($this->TypeInfos['reid'],false);
                }
                return $this->valuePositionName;
            }
        }
    }
    
    
    //获得某个类目的超链接信息
    protected function GetOneTypeLink($typeinfos){
        $typepage = $this->GetOneTypeUrl($typeinfos);
        $typelink = "<a href='".$typepage."'>".$typeinfos['typename']."</a>";
        return $typelink;
    }
    
    //获得某分类连接的URL
    protected function GetOneTypeUrl($typeinfos){
        return GetTypeUrl($typeinfos['id'],MfTypedir($typeinfos['typedir']),$typeinfos['isdefault'],$typeinfos['defaultname'],
        $typeinfos['ispart'],$typeinfos['namerule2']);
    }
    
        //获得某类目的链接列表，递归逻辑部分
    protected function LogicGetPosition($id,$islink){
        $tinfos = D("Contype")->getTypeInfos($id);
        if($islink)
        {
            $this->valuePosition = $this->GetOneTypeLink($tinfos).$this->SplitSymbol.$this->valuePosition;
        }
        else
        {
            $this->valuePositionName = $tinfos['typename'].$this->SplitSymbol.$this->valuePositionName;
        }
        if($tinfos['reid']>0)
        {
            $this->LogicGetPosition($tinfos['reid'],$islink);
        }
        else
        {
            return 0;
        }

    }

   
    /**
     * 得到数据分页
     * @param type $modelName 模型名称
     * @param type $where     分页条件
     * @param type $fields      
     * @param type $order   
     * @return type
     */
    protected function getPagination($modelName, $where, $fields, $order) {
        $Model = D($modelName);
        // 总数据行数
        $totalRows = $Model->getCount($where);
        // 实例化分页
        $page = new \Org\Util\Page($totalRows, C('PAGE_SIZE'));
        $result['page'] = $page->show();
        // 得到分页数据
        $data = $Model->getPagination($where,
                                        $fields,
                                        $order,
                                        $page->firstRow,
                                        $page->listRows);
        $result['data'] = $data;
        $result['total_rows'] = $totalRows;
        return $result;
    }
    
    /**
     * 设置当前语言
     * @return boolean
     */
    public function setLanguage() {
        $lang = I("get.l");
        if(!empty($lang)){
            $lang = TagCheckLang($lang);
            cookie("think_language", $lang);
        }
        return true;
    }
    /**
     * 空操作
     * @return
     */
    public function _empty() {
        header("HTTP/1.0 404 Not Found");
	$this->show('<b>404 Not Found</b>');
    }
}
